package com.ruoyi.hr.service.impl.jobadmin;


import com.ruoyi.common.annotation.DataSource;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.enums.DataSourceType;
import com.ruoyi.framework.util.ShiroUtils;
import com.ruoyi.hr.domain.Authority;
import com.ruoyi.hr.mapper.ProcedureMapper;
import com.ruoyi.hr.mapper.SkyMsgAlarmMapper;
import com.ruoyi.hr.mapper.jobadmin.JobAdminMapper;
import com.ruoyi.hr.service.jobadmin.JobAdminService;
import com.ruoyi.hr.util.BaseResult;
import com.ruoyi.hr.util.ResultEnum;
import com.ruoyi.hr.util.utils;
import com.ruoyi.system.domain.SysUser;
import com.ruoyi.system.mapper.SysUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;


@Service
public class JobAdminServiceImpl extends BaseController implements JobAdminService {

    @Autowired
    JobAdminMapper jobAdminMapper;

    @Autowired
    SysUserMapper sysUserMapper;

    @Autowired
    ProcedureMapper procedureMapper;


    @Autowired
    SkyMsgAlarmMapper skyMsgAlarmMapper;


    @DataSource(value = DataSourceType.SLAVE)
    @Override
    public List<Map<String, Object>> findJobList(Map map, Authority authority) {
        SysUser user = ShiroUtils.getSysUser();
        Map<String, Object> maps = sysUserMapper.selectSkySecUser(user.getLoginName());
        String urid = maps.get("id").toString();
        map.put("urid", urid);
        startPage();
        return jobAdminMapper.findJobList(map, authority);
    }

    @DataSource(value = DataSourceType.SLAVE)
    @Override
    public List<Map<String, Object>> findYxJob() {
        return jobAdminMapper.findYxJob();
    }

    @DataSource(value = DataSourceType.SLAVE)
    @Override
    public int insertJob(Map map) {
        return jobAdminMapper.insertJob(map);
    }

    @DataSource(value = DataSourceType.SLAVE)
    @Override
    public Map<String, Object> findJobAdd(String id) {
        SysUser user = ShiroUtils.getSysUser();
        Map<String, Object> maps = sysUserMapper.selectSkySecUser(user.getLoginName());
        String urid = maps.get("id").toString();
        return jobAdminMapper.findJobAdd(id, urid);
    }

    @DataSource(value = DataSourceType.SLAVE)
    @Override
    public List<Map<String, Object>> findXsDep(String compId) {
        return jobAdminMapper.findXsDep(compId);
    }

    @DataSource(value = DataSourceType.SLAVE)
    @Override
    public int updateJobAdd(Map map) {
        return jobAdminMapper.updateJobAdd(map);
    }


    @DataSource(value = DataSourceType.SLAVE)
    @Override
    public int delete(String ids) {
        String[] idSplit = ids.split(",");
        int num = 0;
        for (String id : idSplit) {
            num += jobAdminMapper.delete(id);
        }
        return num;
    }


    @DataSource(value = DataSourceType.SLAVE)
    @Override
    public BaseResult procedure(String allId, String procedure) {
        //叠加异常信息
        String msgAdd = "";
        try {
            //存储过程sql
            String sql = "";
            SysUser user = ShiroUtils.getSysUser();
            Map<String, Object> maps = sysUserMapper.selectSkySecUser(user.getLoginName());
            String[] array = allId.split(",");
            String msg;//异常信息
            for (String id : array) {
                //根据id取出人名
                String name = jobAdminMapper.findTitleById(id);
                if (procedure.equals("oSP_JobCheck")) {
                    sql = "declare @RetVal varchar(50) exec oSP_JobCheck " + id + "," + maps.get("id").toString() + ",@RetVal output select @RetVal";
                } else if (procedure.equals("oSP_Jobcancel")) {
                    sql = "declare @RetVal varchar(50) exec oSP_Jobcancel " + id + "," + maps.get("id").toString() + ",@RetVal output select @RetVal";
                } else if (procedure.equals("oSP_Jobstart")) {
                    sql = "declare @RetVal varchar(50) exec oSP_Jobstart " + id + "," + maps.get("id").toString() + ",@RetVal output select @RetVal";
                }
                msg = "";
                List<List<Map<String, Object>>> retVal = procedureMapper.ExProcedure(sql);
                //如果大小为1直接取出返回值，先判断是否有key,再判断是否为数字执行
                if (retVal.size() == 1) {
                    if (((Map) retVal.get(0)).containsKey("")) {
                        String by = ((Map) retVal.get(0)).get("").toString();
                        if (utils.isInteger(by)) {
                            msg = skyMsgAlarmMapper.getMsgById(((Map) retVal.get(0)).get("").toString());
                        }
                    }
                } else {
                    for (List<Map<String, Object>> list : retVal) {
                        for (Map<String, Object> map : list) {
                            if (map.containsKey("")) {
                                String by = map.get("").toString();
                                if (utils.isInteger(by)) {
                                    msg += skyMsgAlarmMapper.getMsgById(map.get("").toString());
                                }
                            }
                        }
                    }
                }
                if (msg != null && !msg.equals("") && !msg.equals("null")) {
                    msgAdd += name + ":" + msg + "\n";
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            return BaseResult.build(ResultEnum.SERVER_ERROR);
        }
        if ("".equals(msgAdd)) {

            if (procedure.equals("oSP_JobCheck")) {
                return BaseResult.build(ResultEnum.SERVER_ERROR, "数据确认成功!");
            } else if (procedure.equals("oSP_Jobcancel")) {
                return BaseResult.build(ResultEnum.SERVER_ERROR, "取消确认成功!");
            } else if (procedure.equals("oSP_Jobstart")) {
                return BaseResult.build(ResultEnum.SERVER_ERROR, "执行操作成功!");
            }
            return BaseResult.build(ResultEnum.SUCCESS);
        } else {
            return BaseResult.build(19970926, msgAdd, null);
        }
    }


    @DataSource(value = DataSourceType.SLAVE)
    @Override
    public List<Map<String, Object>> findEditJobList(Map map, Authority authority) {
        SysUser user = ShiroUtils.getSysUser();
        Map<String, Object> maps = sysUserMapper.selectSkySecUser(user.getLoginName());
        String urid = maps.get("id").toString();
        map.put("urid", urid);
        startPage();
        return jobAdminMapper.findEditJobList(map, authority);
    }

    @DataSource(value = DataSourceType.SLAVE)
    @Override
    public int updateJobEdit(Map map) {
        return jobAdminMapper.updateJobEdit(map);
    }

    @DataSource(value = DataSourceType.SLAVE)
    @Override
    public BaseResult addEditJob(String allJob, String type) {
        //叠加异常信息
        String msgAdd = "";
        try {
            //存储过程sql
            String sql;
            SysUser user = ShiroUtils.getSysUser();
            Map<String, Object> maps = sysUserMapper.selectSkySecUser(user.getLoginName());
            String[] array = allJob.split(",");
            String msg;//异常信息
            for (String s : array) {
                //根据PEID取出人名
                sql = "declare @RetVal varchar(50) exec osp_jobadd1 " + s + "," + type + "," + maps.get("id").toString() + ",@RetVal output select @RetVal";
                String retVal = procedureMapper.executeProcedure(sql);
                msg = skyMsgAlarmMapper.getMsgById(retVal);
                if (msg != null) {
                    msgAdd += msg + "\n";
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            return BaseResult.build(ResultEnum.SERVER_ERROR);
        }
        if ("".equals(msgAdd)) {
            return BaseResult.build(ResultEnum.SUCCESS);
        } else {
            return BaseResult.build(19970926, msgAdd, null);
        }
    }

    @DataSource(value = DataSourceType.SLAVE)
    @Override
    public int editDepInvalid(Map map) {
        return jobAdminMapper.editDepInvalid(map);
    }

    @DataSource(value = DataSourceType.SLAVE)
    @Override
    public List<Map<String, Object>> findHistoryJobList(Map map,Authority authority) {
        SysUser user = ShiroUtils.getSysUser();
        Map<String, Object> maps = sysUserMapper.selectSkySecUser(user.getLoginName());
        String urid = maps.get("id").toString();
        map.put("urid", urid);
        startPage();
        return jobAdminMapper.findHistoryJobList(map,authority);
    }

    @DataSource(value = DataSourceType.SLAVE)
    @Override
    public List<Map<String, Object>> jobInfoList(Map map, Authority authority) {
        SysUser user = ShiroUtils.getSysUser();
        Map<String, Object> maps = sysUserMapper.selectSkySecUser(user.getLoginName());
        String urid = maps.get("id").toString();
        map.put("urid", urid);
        startPage();
        return jobAdminMapper.jobInfoList(map,authority);
    }
}
